﻿package com.game.utils
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	
	public class LoadSwf extends Object
	{

		private var _error:Function;
		private var _prog:Function;
		private var _onCom:Function;
		private var _url:String;
		private var _loader:Loader;
		
		public function LoadSwf()
		{
			
		}
		public function setLoad(url:String, onCom:Function=null, error:Function = null, prog:Function = null):void{
			_url = url;
			_onCom = onCom;
			_prog=prog;
			_error = error;
			_loader = new Loader();
			_loader.load(new URLRequest(url));
			_loader.contentLoaderInfo.addEventListener(Event.OPEN, this.imgSwfOpen);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.imgSwfProgress);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.imgSwfError);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.imgSwfComplete);
		}
		private function imgSwfOpen(event:Event) : void
		{
		}
		
		private function imgSwfProgress(event:ProgressEvent) : void
		{
			var _per:uint=Math.floor(100*event.bytesLoaded/event.bytesTotal);
			if(_prog!=null){
				_prog(_per);
			}
		}
		
		private function imgSwfError(event:Event) : void
		{
			trace("无法加载" + this._url);
			if(_error!=null){
				_error();
			}
			delEvent();
		}
		
		private function imgSwfComplete(event:Event) : void
		{
			if(_onCom!=null){
				_onCom(event.target as LoaderInfo);
			}
			delEvent();
		}
		
		private function delEvent() : void
		{
			
			_loader.contentLoaderInfo.removeEventListener(Event.OPEN, this.imgSwfOpen);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, this.imgSwfProgress);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, this.imgSwfError);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.imgSwfComplete);
			
		}
		
		
	}
}
